package com.yw.common.core.db.test.jdbctemplate;

import java.sql.PreparedStatement;

import com.yw.common.core.db.IDBConnection;


public abstract class PreparedStatementCallback<T> implements IJdbcCallback<T>{

	public abstract Object[] getParams();
	public abstract String getSql();
	public abstract T execute(PreparedStatement ps);
	
	@Override
	public T execute(IDBConnection conn) {
		PreparedStatement pstm = null;
		Object[] params = this.getParams();
		T res = null;
		try {
			conn.open();
			pstm = conn.prepareStatement(this.getSql());
			
			for(int i = 0;i < params.length;i++){
				pstm.setObject(i + 1, params[i]);
			}
			res = this.execute(pstm);
			
			conn.close();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return res;
	}
	
	
	
}
